// manager/components/ImageDataUpload/index.js
import config from '../../../utils/config';
import { request } from '../../../utils/request';
Component({

    /**
     * 组件的属性列表
     */
    properties: {
        required: {
			type: Boolean,
			value: false
		},
		title: {
			type: String,
			value: ""
		},
		docCode: {
			type: String,
			value: ""
		},
		docRegId: {
			type: [String, Number],
			value: ""
		},
		list: {
			type: Array,
			value () {
				return []
			}
		},
		index: {
			type: Number,
			dafault: 0
		},
		valueKey: {
			type: String,
			dafault: "fileUrl"
		},
		maxLength: {
			type: Number,
			value: 100
		},
		showDelete: {
			type: Boolean,
			value: true
		},
		showUpload: {
			type: Boolean,
			value: true
		}
    },

    /**
     * 组件的初始数据
     */
    data: {
        baseUrl: config.baseUrl,
        // 定位地址
        address: ""
    },

    /**
     * 组件的方法列表
     */
    methods: {
        /** 查看图片 */
        previewImage(e) {
            let { url } = e.currentTarget.dataset;
            let { list, valueKey, baseUrl } = this.data;
            let images = [];
            list.forEach(item => {
            	images.push(baseUrl + item[valueKey]);
            })
            wx.previewImage({
                current: url, // 当前显示图片的http链接
                urls: images// 需要预览的图片http链接列表
            })
        },
        /** 上传图片 */
		chooseImage() {	
            let that = this;
            wx.chooseImage({
                count: 1, // 默认9
                sizeType: ['compressed', 'original'],
                sourceType: ['camera', 'album'],
                success(res) {
                    wx.compressImage({
                        src: res.tempFilePaths[0], // 图片路径
                        quality: 40, // 压缩质量
                        success(res1) {
                            that.triggerEvent('upload',{
                                index: that.data.index,
                                location: that.data.address,
                                file: res1.tempFilePath,
                                docCode: that.data.docCode,
                                docRegId: that.data.docRegId
                            })
                        }
                    })
                }
            })
        },
        /**
		 *  删除图片
		 */
		deleteImage(e){
			let { item, index } = e.currentTarget.dataset;
            //this.deleteItem = item;
            this.triggerEvent('delete',{
                index: this.data.index,
                deleteIndex: index,
				item: item
            })
        },
        
        // 获取经纬度
        getLatLng(){
            let that = this;
            console.log(this.data.address)
            if(this.data.address){
                this.chooseImage()
            }else{
                wx.getSetting({
                    success: (res) => {
                        console.log(res)
                        if (res.authSetting['scope.userLocation'] == false) {//非初始化进入该页面,且未授权
                            wx.showModal({
                                title: '是否授权当前位置',
                                content: '需要获取您的地理位置，请确认授权',
                                success: function (res) {
                                    if (res.confirm) {
                                        wx.openSetting({
                                            success: function (data) {
                                                // console.log(data);
                                                if (data.authSetting["scope.userLocation"] == true) {
                                                    that.getLocation();
                                                }
                                            }
                                        })
                                    }
                                }
                            })
                        } else {//初始化进入
                            // console.log("KKKKK") 
                            that.getLocation();
                        }
                    }
                })
            }
        },
        getLocation(){
            const that = this;
            wx.getLocation({
                type: 'gcj02',
                altitude: true,
                isHighAccuracy: true,
                success (res) {
                    that.setData({
                        latitude: res.latitude,
                        longitude: res.longitude,
                    })
                    that.getCoordinatePositionInfo(res.latitude,res.longitude)
                },
                complete: function(res) {
                    console.log(res)
                    if(res.errMsg == "getLocation:fail auth deny"){
                        // wx.showToast({
                        //     title: '获取定位失败',
                        //     icon: 'none',
                        //     duration: 2000
                        // });
                    }
                }
            })
        },
        getCoordinatePositionInfo(latitude,longitude){
            let { form } = this.data;
            request({
                url: "/commonApi/getCoordinatePositionInfo",
                method: "GET",
                data: {
                    coordinate: `${latitude},${longitude}`
                }
            }).then(res => {
                if(res.code == 0){
                    let address = res.data.address;
                    this.setData({
                        address,
                    })
                    this.chooseImage();
                }
            }).catch(error => {
                console.log(error)
            })
        },
    }
})